<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Agent extends CI_Controller {
	function __construct()
    {
        parent::__construct();
        if(in_array($this->uri->segment( 2 , 0 ),array('login','doLogin','logout')))
        {
        	return true;
        }
        if (!($this->_isLogIn()))
        {
        	redirect('/agent/login');
        }
    }
    function _isLogIn()
    {
	    if ($this->session->userdata('agent'))
		{
			return true;
		}
		else
		{
			return false;
		}
    }
	public function index()
	{
		if (_gs('agent'))
		{
			$this->main();
		}
		else
		{
			$this->login();
		}
	}
	public function desktop()
	{
		$new_orders	= $verifing_orders	= $verified_orders	= $unverified_orders	= $expire_orders	= 0;
		$agent_id	= _gs('agent_id');
//		if(!($agent_id)>0)
//		{
//			redirect('/agent/login');
//			return false;
//		}
		$sql	= "SELECT count(0) as num,flag FROM customer where agent_id='$agent_id'  group by flag";
		$query	= $this->db->query($sql);
		if ($query && $query->num_rows()>0)
		{
			foreach ($query->result() as $row)
			{
				if($row->flag == -1)
				{
					$unverified_orders	= $row->num;
				}
				elseif($row->flag == 0)
				{
					$new_orders	= $row->num;
				}
				elseif($row->flag == 1)
				{
					$verifing_orders	= $row->num;
				}
				elseif($row->flag == 2)
				{
					$verified_orders	= $row->num;
				}
			}
		}
		$this->db->from('customer');
		$this->db->where('service_end_time >=', date('Y-m-d', strtotime('-1 month')));
		$this->db->where('agent_id',$agent_id);
		$expire_orders	= $this->db->count_all_results();
		$data['header']	= array('islogin'=>false);
		$data['body']	= array(
			'new_orders' => $new_orders, 
			'verifing_orders' => $verifing_orders, 
			'verified_orders' => $verified_orders,
			'unverified_orders' => $unverified_orders,
			'expire_orders' => $expire_orders);
		showpage($data, 'customers/desktop');
	}
	public function updatePasswd()
	{
		$old_passwd	= trim(_pv('passwd'));
		$new_passwd	= trim(_pv('newpasswd'));
		$agent_id	= _gs('agent_id');
		$this->db->from('agent');
		$this->db->where('id', $agent_id);
		$query	= $this->db->get();
		$row	= $query->row();
		if ($row->pwd == md5($old_passwd))
		{
			$this->db->update('agent', array('pwd'=>md5($new_passwd)), array('id'=>$agent_id));
			redirect('/agent');
		}
		redirect('/agent/chgpwd');
	}
	public function chgpwd()
	{
		$data['header']	= array('islogin'=>false);
		$data['body']	= array();
		showpage($data, 'customers/chgpasswd');
	}
	public function login()
	{
		$data['header']	= array('islogin'=>true);
		$data['body']	= array();
		showpage($data, 'customers/login');
	}
	public function doLogin()
	{
		$username	= trim(_pv('username'));
		$passwd	= _pv('passwd');
		$this->db->from('agent');
		$this->db->where('name', $username);
		$query	= $this->db->get();
		
		if ($query && $query->num_rows() > 0)
		{
			$row	= $query->row();
			if($row->status==-1)
			{
				redirect('/agent');
				return false;
			}
			if ($row->pwd == md5($passwd))
			{
				$this->session->set_userdata('agent', $row->name);
				$this->session->set_userdata('nickname', $row->nickname);
				$this->session->set_userdata('agent_id', $row->id);
			}
		}
		redirect('/agent');
	}
	public function logout()
	{
		$this->session->sess_destroy();
		redirect('/agent');
	}
	public function search()
	{
		$agent_id	= _gs('agent_id');
		$name		= _pv('search_customer_name');
		$domain	= _pv('search_domain_name');
		$this->db->from('customer');
		if ($name )
		{
			$this->db->like('customer_name', $name);
		}
		if ($domain)
		{
			$this->db->like('domain', $domain);
		}
		$this->db->where('flag >', '-2');
		$this->db->where('agent_id',$agent_id);
		$this->db->order_by('id', 'desc');
		$list	= $this->db->get();
		$data['body']	= array('lists' => $list, 'search_name'=>$name, 'search_domain'=>$domain);
		showpage($data, 'customers/searchList');
	}
	public function main()
	{
		$offset	= _uk(3);
		$num	= 10;
		$agent_id	= _gs('agent_id');
		$data['header']	= array('islogin' => false);
		$this->db->from('customer');
		$this->db->where('flag >', '-2');
		$this->db->where('agent_id',$agent_id);
		$this->db->limit($num,$offset);
		$this->db->order_by('id', 'desc');
		$list	= $this->db->get();
		$this->db->from('customer');
		$this->db->where('flag >', '-2');
		$this->db->where('agent_id',$agent_id);
		$total_num	= $this->db->count_all_results();
		$pages	= genPageLinkNav($total_num,'/agent/main/',3,$num);
		$data['body']	= array('lists' => $list,'pages'=>$pages);
		showpage($data, 'customers/main');
	}
	public function delete()
	{
		$agent_id	= _gs('agent_id');
		$id	= _uk(3);
		$this->db->update('customer', array('flag' => -2), array('id'=>$id,'agent_id'=>$agent_id));
		redirect('/agent');
	}
	public function add()
	{
		$data['header']	= array('islogin' => false);
		$data['body']	= array();
		showpage($data, 'customers/addCustomer');
	}
	public function verifing()
	{
		$agent_id	= _gs('agent_id');
		$offset	= _uk(3);
		$num	= 10;
		$data['header']	= array('islogin' => false);
		$this->db->from('customer');
		$this->db->where('flag', 1);
		$this->db->where('agent_id',$agent_id);
		$this->db->limit($num,$offset);
		$this->db->order_by('id', 'desc');
		$list	= $this->db->get();
		$this->db->from('customer');
		$this->db->where('flag', 1);
		$this->db->where('agent_id',$agent_id);
		$total_num	= $this->db->count_all_results();
		$pages	= genPageLinkNav($total_num,'/agent/verifing/',3,$num);
		$data['body']	= array('lists' => $list,'pages'=>$pages);
		showpage($data, 'customers/verifing');
	}
	public function verified()
	{
		$agent_id	= _gs('agent_id');
		$offset	= _uk(3);
		$num	= 10;
		$data['header']	= array('islogin' => false);
		$this->db->from('customer');
		$this->db->where('flag', 2);
		$this->db->where('agent_id',$agent_id);
		$this->db->limit($num,$offset);
		$this->db->order_by('id', 'desc');
		$list	= $this->db->get();
		$this->db->from('customer');
		$this->db->where('flag', 2);
		$this->db->where('agent_id',$agent_id);
		$total_num	= $this->db->count_all_results();
		$pages	= genPageLinkNav($total_num,'/agent/verified/',3,$num);
		$data['body']	= array('lists' => $list,'pages'=>$pages);
		showpage($data, 'customers/verified');
	}
	public function unverified()
	{
		$agent_id	= _gs('agent_id');
		$offset	= _uk(3);
		$num	= 10;
		$data['header']	= array('islogin' => false);
		$this->db->from('customer');
		$this->db->where('flag', -1);
		$this->db->where('agent_id',$agent_id);
		$this->db->limit($num,$offset);
		$this->db->order_by('id', 'desc');
		$list	= $this->db->get();
		$this->db->from('customer');
		$this->db->where('flag', -1);
		$this->db->where('agent_id',$agent_id);
		$total_num	= $this->db->count_all_results();
		$pages	= genPageLinkNav($total_num,'/agent/unverified/',3,$num);
		$data['body']	= array('lists' => $list,'pages'=>$pages);
		showpage($data, 'customers/verified');
	}
	public function addCustomer()
	{
		$data['customer_id']	= _pv('customer_id');
		$data['customer_create_time']	= _pv('create_time');
		$data['order_type']	= _pv('customer_id');
		$data['order_status']	= _pv('customer_id');
		$data['agent_id']	= _gs('agent_id');
		$data['agent_name']	= _gs('agent');
		$data['domain_status']	= _pv('domain_status');
		$data['domain']	= _pv('domain_name');
		$data['customer_name']	= _pv('company_name');
		$data['customer_province']	= _pv('province');
		$data['customer_city']	= _pv('city');
		$data['customer_big_area']	= _pv('customer_big_area');
		$data['customer_industry']	= _pv('industry');
		$data['customer_size']	= _pv('company_size');
		$data['customer_address']	= _pv('address');
		$data['customer_zip']	= _pv('zip');
		$data['primary_contact_name']	= _pv('primary_contact');
		$data['primary_contact_telphone']	= _pv('primary_telphone');
		$data['primary_contact_mobile']	= _pv('primary_mobile');
		$data['contact_name']	= _pv('contact_name');
		$data['contact_telphone']	= _pv('telphone');
		$data['contact_mobile']	= _pv('mobile');
		$data['contact_note']	= _pv('note');
		$data['info_from']	= _pv('info_from');
		$data['info_from_type']	= _pv('info_from_type');
		$data['info_istest']	= _pv('testOrder');
		$data['info_status']	= _pv('info_status');
		$data['info_user_nums']	= _pv('user_nums');
		$data['info_userway1']	= _pv('useWay1');
		$data['info_userway2']	= _pv('useWay2');
		$data['info_old_service_end_time']	= _pv('old_service_endtime');
		$data['info_old_service_price']	= _pv('old_service_price');
		$data['info_against']	= '';
		$data['info_functions']	= '';
		$data['primary_contact_qq']	= _pv('qq');
		$data['primary_contact_msn']	= _pv('msn');
		$data['primary_contact_email']	= _pv('email');
		if (!empty($_POST['companys']))
		{
			$data['info_against']	= join('|', _pv('companys'));
		}
		
		if (!empty($_POST['functions']))
		{
			$data['info_functions']	= join('|', _pv('functions'));
		}
		if ($this->_checkDomain($data['domain']))
		{
			redirect('/agent/add');
		}
		$data['info_sign_time']	= _pv('info_sign_time');
		$data['info_sales_status']	= '待选择';
		$this->db->insert('customer', $data);
		//echo $this->db->insert_string('customer', $data);
		//die();
		redirect('/agent');
		
	}
	private function _checkDomain($domain)
	{
		$this->db->from('customer');
		$this->db->where('domain', $domain);
		$this->db->where('flag >' , -2);
		$query  = $this->db->get();
		if ($query && $query->num_rows() > 0)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	public function edit()
	{
		$id	= _uk(3);
		$data['header']	= array('islogin' => false);
		$this->db->from('customer');
		$this->db->where('id', $id);
		$query	= $this->db->get();
		$row	= $query->row();
		$data['body']	= array('info' => $row);
		showpage($data, 'customers/editCustomer');
	}
	public function updateCustomer()
	{
		$where['id']	= _pv('id');
		$data['customer_id']	= _pv('customer_id');
		$data['customer_create_time']	= _pv('create_time');
		$data['order_type']	= _pv('customer_id');
		$data['order_status']	= _pv('customer_id');
		$data['agent_id']	= _gs('agent_id');
		$data['agent_name']	= _gs('agent');
		$data['domain_status']	= _pv('domain_status');
		$data['domain']	= _pv('domain_name');
		$data['customer_name']	= _pv('company_name');
		$data['customer_province']	= _pv('province');
		$data['customer_city']	= _pv('city');
		$data['customer_big_area']	= _pv('customer_big_area');
		$data['customer_industry']	= _pv('industry');
		$data['customer_size']	= _pv('company_size');
		$data['customer_address']	= _pv('address');
		$data['customer_zip']	= _pv('zip');
		$data['primary_contact_name']	= _pv('primary_contact');
		$data['primary_contact_telphone']	= _pv('primary_telphone');
		$data['primary_contact_mobile']	= _pv('primary_mobile');
		$data['contact_name']	= _pv('contact_name');
		$data['contact_telphone']	= _pv('telphone');
		$data['contact_mobile']	= _pv('mobile');
		$data['contact_note']	= _pv('note');
		$data['info_from']	= _pv('info_from');
		$data['info_from_type']	= _pv('info_from_type');
		$data['info_istest']	= _pv('testOrder');
		$data['info_status']	= _pv('info_status');
		$data['info_user_nums']	= _pv('user_nums');
		$data['info_userway1']	= _pv('useWay1');
		$data['info_userway2']	= _pv('useWay2');
		$data['info_old_service_end_time']	= _pv('old_service_endtime');
		$data['info_old_service_price']	= _pv('old_service_price');
		$data['info_against']	= '';
		$data['info_functions']	= '';
		$data['primary_contact_qq']	= _pv('qq');
		$data['primary_contact_msn']	= _pv('msn');
		$data['primary_contact_email']	= _pv('email');
		if (_pv('service_end_time'))
		{
			$data['service_end_time']	= _pv('service_end_time');
		}
		if (_pv('service_start_time'))
		{
			$data['service_start_time']	= _pv('service_start_time');
		}
		if (!empty($_POST['companys']))
		{
			$data['info_against']	= join('|', _pv('companys'));
		}
		
		if (!empty($_POST['functions']))
		{
			$data['info_functions']	= join('|', _pv('functions'));
		}
		if (_pv('flag'))
		{
			$data['flag']	= _pv('flag');
		}
		$data['info_sign_time']	= _pv('info_sign_time');
		//$data['info_sales_status']	= '待选择';
		$this->db->update('customer', $data, $where);
		//echo $this->db->insert_string('customer', $data);
		//die();
		redirect('/agent');
		
	}
	public function bat_delete_customer()
	{
		$agent_id	= _gs('agent_id');
		$ids	= _uk(3);
		$ids	= substr($ids,0,-1);
		$ids	= str_replace('-',',',$ids);
		$this->db->update('customer', array('flag' => -2), "id IN ($ids) AND agent_id='$agent_id'");
		redirect('/agent');
	}
	public function bat_edit_cusomer()
	{
		$agent_id	= _gs('agent_id');
		$status	= _uk(3);
		$ids	= substr(_uk(4),0,-1);
		$ids	= str_replace('-',',',$ids);
		$this->db->update('customer',array('flag'=>$status),"id IN ($ids) AND agent_id='$agent_id'");
		switch($status)
		{
			case -1:
				redirect('/agent/unverified/');
				break;
			case 1:
				redirect('/agent/verifing/');
				break;
			case 2:
				redirect('/agent/verified/');
				break;
			default:
				redirect('/agent/');
				break;
		}
	}
	/**
	 * 显示订单详情
	 */
	public function showCustomerDetail()
	{
		$cid	= _uk(3);
		$this->db->from('customer');
		$this->db->where(array('id'=>$cid));
		$res	= $this->db->get();
		$data['header']	= array('islogin' => false);
		$data['body']['info']	= $res->row();
		showpage($data,'customers/showDetail');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */